<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<c-rt:set var="midContextPath" value='<%=ServerValue.getMidContextPath()%>'/>
<c-rt:set var="frontContentPath" value='<%=ServerValue.getFrontContextPath()%>'/>

<s:set name="listSize" value="20"></s:set>
<s:iterator value="%{viewReportProductClickList}" var="viewReportProductClick" status="stat">
	<s:if test="%{#stat.count <= #listSize}">
		<tr>
			<td>
				<s:if test="%{!newRecords}">
					<s:property value="%{#stat.count}" />
				</s:if>
			</td>
			<td>
				<img onError="this.src='/PImg/blank_75.gif'" src="/PImg/${fn:substring(viewReportProductClick.primaryKey.productId,0,4)}/${viewReportProductClick.primaryKey.productId}/${viewReportProductClick.primaryKey.productId}_m75.jpg"/>
			</td>
			<td>
				<s:property value="%{#viewReportProductClick.primaryKey.productId}" />
				<s:hidden name="productId" value="%{#viewReportProductClick.primaryKey.productId}" ></s:hidden>
				<s:hidden name="deleteProductId" value="%{#viewReportProductClick.primaryKey.productId}" disabled="true" ></s:hidden>
				<s:hidden name="deleteCategoryId" value="%{#viewReportProductClick.primaryKey.categoryId}" disabled="true" ></s:hidden>
				<s:hidden name="newRecord" value="%{newRecords}" ></s:hidden>
			</td>
			<td>
				<a href="${frontContentPath}/product/product.do?productId=${viewReportProductClick.primaryKey.productId}" target="_blank" />
					<s:property value="#viewReportProductClick.productName" />
				</a>
			</td>
			<td>
				<s:if test="#viewReportProductClick.shelfPrice != null">
					￥
					<fmt-rt:formatNumber pattern="###,###,##0.0">
						<s:property value="#viewReportProductClick.shelfPrice" />
					</fmt-rt:formatNumber>
				</s:if>
			</td>
			<td>
				<script type="text/javascript" charset="UTF-8" >
					function moveUp(anchor) {
						//上移
						if (anchor.parents("tr:first").prev("tr").length <= 0) {
							return false;
						}
						var row = anchor.parents('tr:first');
						row.insertBefore(row.prev());
						
						controlMoveButtonVisible();
						
						return false;
					}
					
					function moveDown(anchor) {
						//下移
						if (anchor.parents("tr:first").next("tr").length <= 0) {
							return false;
						}
						var row = anchor.parents('tr:first');
						row.insertAfter(row.next());

						controlMoveButtonVisible();
						
						return false;
					}
					
					function deleteRow(anchor) {
						//刪除
						var row = anchor.parents('tr:first');
						row.hide();
						var deleteProductIdInput = row.find("input[name='deleteProductId']");
						var deleteCategoryIdInput = row.find("input[name='deleteCategoryId']");
						var saveProductIdInput = row.find("input[name='productId']");
						var newRecordMark = row.find("input[name='newRecord']");
						deleteProductIdInput.attr("disabled", false);
						deleteCategoryIdInput.attr("disabled", false);
						saveProductIdInput.attr("disabled",true);
						newRecordMark.attr("disabled", true);
						
						controlMoveButtonVisible();
						
						return false;
					}
					
					function insertProductRow(anchor) {
						//插入
						var insertClickedRowMarkClass = "insertClickedRows";
						var productSelectWindow = $("#productSelectWindow");
						var loadingDialog = $("#loadingImg");
				
						anchor.parents('tr:first').addClass(insertClickedRowMarkClass);//用 class 標記是哪一行按下插入，才知道選取的商品要插到哪裡
						
						var categoryId = anchor.next("input:first").val();
						$.ajax({
						    url: '${midContextPath}/reportProductClick/productSelect/productSearch.do',
						    type: 'post',
						    data: {categoryId:categoryId},//第二層 categoryId，查詢所有這個第二層 categoryId 下面的 category 的商品
						    cache: false,
						    beforeSend: function( strData ){
						    	loadingDialog.dialog('open');
							},
						    success: function( strData ){
						    	loadingDialog.dialog('close');
						    	productSelectWindow.html(strData);
						    	productSelectWindow.dialog('open');
							}
						});	  
						
						controlMoveButtonVisible();
						
						return false;
					}
					
					function controlMoveButtonVisible() {
						
						var moveUpButtonInVisibleRows = $("tr:visible .newMoveUpButton");
						moveUpButtonInVisibleRows.each(function(n) {
							if (n == 0) {
								$(this).hide();
								$(this).next("br").hide();
							} else {
								$(this).show();
								$(this).next("br").show();
							}
						});
						
						var moveDownButtonInVisibleRows = $("tr:visible .newMoveDownButton");
						moveDownButtonInVisibleRows.each(function(n) {
							if (n == (moveDownButtonInVisibleRows.size() - 1)) {
								$(this).hide();
								$(this).next("br").hide();
							} else {
								$(this).show();
								$(this).next("br").show();
							}
						});
					}
					
					$(function() {
						controlMoveButtonVisible();
					});
				</script>
				<a class="newMoveUpButton" onClick="return moveUp($(this));" href="#">↑上移</a>
				<br/>
				
				<a class="newInsertButton" onClick="return insertProductRow($(this))" href="#">←插入</a>
				<!--這裡放的是第二層的 categoryId，查詢時要查詢所有這個第二層 categoryId 下面的 category 的商品-->
				<!--這個 hidden 只供 javascript 用，不 submit 出去-->
				<s:hidden name="categoryId" value="%{categoryId}" disabled="true" ></s:hidden>
				
				<br/>
				<a class="newMoveDownButton" onClick="return moveDown($(this))" href="#">↓下移</a>
				<br/>
				
				<a class="newDeleteButton" onClick="return deleteRow($(this))" href="#">
					<font color="red">
						☒删除
					</font>
				</a>
			</td>
		</tr>
	</s:if>
</s:iterator>
